﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using LY.Common.Utils.Sf_Struct;
using LY.ShengPayApi.Model;
using Newtonsoft.Json;

namespace LY.ShengPayApi.Services
{
    public class Get
    {
        /// <summary>
        /// 获取商户余额
        /// </summary>
        /// <param name="serverUrl">接口地址</param>
        /// <param name="merchantNo">商户编码</param>
        /// <param name="relatedMerchantNo">和平台关联的商户号</param>
        /// <returns></returns>
        public static MerchantAccount GetMerchantAccount(string serverUrl, string merchantNo, string relatedMerchantNo)
        {
            string sign = "merchantNo=" + merchantNo + "&relatedMerchantNo=" + relatedMerchantNo;

            //签名
            var mysign = new MySign();
            string temp = mysign.GetSign(sign);
            string newsign = HttpContext.Current.Server.UrlEncode(temp);

            //url拼接
            string postString = "?sign=" + newsign;
            string url = serverUrl + "/merchantAccount/" + merchantNo + "-" + relatedMerchantNo + postString;

            // 初始化WebClient
            //  var webClient = new WebClient();
            //  webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            //  // 要提交表单的URI字符串。
            //  string postString = "?sign=" + newsign;
            ////  string url = "http://wdtest.shengpay.com/api/merchantAccount/" + merchantNo + "-" + relatedMerchantNo + postString;
            //  string url =serverUrl+"/merchantAccount/" + merchantNo + "-" + relatedMerchantNo + postString;
            //  // 将字符串转换成字节数组-
            //  byte[] postData = Encoding.ASCII.GetBytes(postString);
            //  // 上传数据，返回页面的字节数组
            //  byte[] responseData = webClient.DownloadData(url);
            //  // 返回的将字节数组转换成字符串(HTML)
            //  string strJson = Encoding.UTF8.GetString(responseData); 


            string strJson = HttpGet(url, postString);
            var m = JsonConvert.DeserializeObject<MerchantAccount>(strJson);
            return m;
        }

        /// <summary>
        /// 查询用户余额
        /// </summary>
        /// <param name="serverUrl"></param>
        /// <param name="merchantNo"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static UserAccount GetUserAccount(string serverUrl, string merchantNo, string userName)
        {
            string sign = "merchantNo=" + merchantNo + "&userName=" + userName;
            //签名
            var mysign = new MySign();
            string temp = mysign.GetSign(sign);
            string newsign = HttpContext.Current.Server.UrlEncode(temp);
            //url拼接
            string postString = "?sign=" + newsign;
            string url = serverUrl + "/userAccount/" + merchantNo + "-" + userName + postString;

            string strJson = HttpGet(url, postString);
            var m = JsonConvert.DeserializeObject<UserAccount>(strJson);
            return m;
        }


        public static string HttpGet(string url, string param)
        {
            var webClient = new WebClient();
            webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            byte[] postData = Encoding.ASCII.GetBytes(param);
            // 上传数据，返回页面的字节数组
            byte[] responseData = webClient.DownloadData(url);
            // 返回的将字节数组转换成字符串(HTML)
            return Encoding.UTF8.GetString(responseData);



        }
    }
}
